package com.hongdee.scm.repertory.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;

import com.hongdee.common.util.StringUtil;
import com.hongdee.scm.Common;
import com.hongdee.scm.repertory.entity.TScmReperory;
import com.hongdee.scm.repertory.service.ITScmRepertoryService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmRepertoryController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.repertory.controller
 * 作  者: 术洪超
 * 日  期:2017-05-31 16:55
 */

@RestController
@RequestMapping("scm/repertory")
public class TScmRepertoryController {

    @Autowired
    private ITScmRepertoryService itScmRepertoryService;

    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data
    ) throws Exception {

        Map map = itScmRepertoryService.page(start, limit, uid, data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    @RequestMapping("/pageApp")
    @ResponseBody
    public Map pageApp(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data
    ) throws Exception {

        Map map = itScmRepertoryService.pageApp(start, limit, uid, data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param ids    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 修改车辆销售状态
     * @author 术洪超
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "saleType", defaultValue = "") String saleType,
                               @RequestParam(name = "saleTypeReason", defaultValue = "") String saleTypeReason,
                               @RequestParam(name = "allocationStatus", defaultValue = "") String allocationStatus,
                               @RequestBody String[] ids

    ) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmRepertoryService.update(ids, saleType, saleTypeReason,allocationStatus);
        res.setSuccess(true);
        res.setMsg("成功修改！");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择车辆！");
        }
        Map map = itScmRepertoryService.info(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 库存报表
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     * @author 术洪超
     */
    @RequestMapping("/repertoryReport")
    @ResponseBody
    public Map repertoryReport(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "radio", defaultValue = "") String radio,
                               @RequestBody Map data
    ) throws Exception {

        Map pageMap = itScmRepertoryService.repertoryReport(radio,uid, data);
        pageMap.put("success", true);
        pageMap.put("msg", "库存查询成功！");
        return pageMap;
    }

    /**
     *
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     * @author 术洪超
     */
    @RequestMapping("/repertoryPage")
    @ResponseBody
    public Map repertoryPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "start", defaultValue = "0") String start,
                             @RequestParam(name = "limit", defaultValue = "50") String limit,
                             @RequestParam(name = "name", defaultValue = "") String name,
                             @RequestParam(name = "types", defaultValue = "") String type,
                             @RequestBody Map data) throws Exception {

        Map pageMap = itScmRepertoryService.reportPage(start, limit, uid, name,type,data);
        pageMap.put("success", true);
        pageMap.put("msg", "库存查询成功！");
        return pageMap;
    }

    /**
     * 库存导出
     * @param uid
     * @param request
     * @throws Exception
     */
    @RequestMapping("/exports")
    @ResponseBody
    public ResultEntity exportRepertory(@RequestParam(name = "uid", defaultValue = "") String uid,
                                         @RequestParam(name = "token", defaultValue = "") String token,
                                         @RequestBody Map data,
                                         HttpServletRequest request) throws Exception {
        List<TScmReperory> dataList = this.itScmRepertoryService.getExcel(uid,data);
        XSSFWorkbook wb = this.itScmRepertoryService.createExcel(dataList, uid);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path =   path + File.separator + "upload";

        File filepack = new File(path);
        if(!filepack.exists()){
            filepack.mkdir();
        }
        String filePath =   path + File.separator + fileId + ".xlsx";
        System.out.println("文件路径：" + filePath);
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }

}
